package com.blinddate.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blinddate.context.BaseContext;
import com.blinddate.mapper.GoodsTypeMapper;
import com.blinddate.mapper.OrdersMapper;
import com.blinddate.mapper.UserMapper;
import com.blinddate.pojo.dto.OrdersPaymentDTO;
import com.blinddate.pojo.entity.GoodsType;
import com.blinddate.pojo.entity.Orders;
import com.blinddate.pojo.entity.User;
import com.blinddate.pojo.vo.OrderPaymentVO;
import com.blinddate.service.OrdersService;
import com.blinddate.utils.WeChatPayUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper,Orders> implements OrdersService{

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private GoodsTypeMapper goodsTypeMapper;

    @Autowired
    private WeChatPayUtil weChatPayUtil;

    @Override
    public void paySuccess(String outTradeNo) {
//        根据订单号获取数据库中的订单信息
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(Orders::getOutTradeNo,outTradeNo);
        Orders orders = ordersMapper.selectOne(ordersLambdaQueryWrapper);
        orders.setStatus(Orders.PAID);
        orders.setCheckoutTime(LocalDateTime.now());
//        更新订单信息
        int update = ordersMapper.update(orders,null);
        if (update==1){
//            获取商品的信息
            LambdaQueryWrapper<GoodsType> goodsTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            goodsTypeLambdaQueryWrapper.eq(GoodsType::getId,orders.getType());
//            获得商品的属性
            GoodsType goodsType = goodsTypeMapper.selectOne(goodsTypeLambdaQueryWrapper);
        }
    }

    @Override
    public OrderPaymentVO payment(OrdersPaymentDTO ordersPaymentDTO) {
        Long currentId = BaseContext.getCurrentId();
        User user = userMapper.selectById(currentId);
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(Orders::getOutTradeNo,ordersPaymentDTO.getOutTradeNo());
        Orders orders = ordersMapper.selectOne(ordersLambdaQueryWrapper);
        try {
            JSONObject pay = weChatPayUtil.pay(ordersPaymentDTO.getOutTradeNo(), new BigDecimal(orders.getTotal()), orders.getDescription(), user.getOpenId());
            if (pay.getString("code")!=null&&pay.getString("code").equals("ORDERPAID")){
                throw new RuntimeException("该订单已经支付");
            }
            OrderPaymentVO orderPaymentVO = pay.toJavaObject(OrderPaymentVO.class);
            orderPaymentVO.setPackageStr(pay.getString("package"));
            return orderPaymentVO;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}
